package frontend.registration.dao;
import core.util.HibernateCoreUtil;
import core.address.model.BasicAddress;
import core.discount.model.Coupon;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import frontend.registration.controller.Encript;
import frontend.user.controller.GetDataByEmailId;

public class RegisterDao {

public static boolean registerUser(BasicCredential bc,BasicProfile bp,BasicAddress ba) {

                   SessionFactory sf = HibernateCoreUtil.getSessionFactory();

                 Transaction t = null;

         try {

                       Session s = sf.openSession();

                      t = s.beginTransaction(); // start a new transaction
                      

                     s.persist(bc);
                     s.persist(bp);
                     s.persist(ba);

                      t.commit(); // commit transaction

                         return true;

              } catch (Exception ex) {

          System.err.println("Error -->" + ex.getMessage());

           if (t != null)

                    t.rollback();

                   return false;

         }

}


public boolean getTravellerUser(String userId,String password)
{
	
	boolean validation = false;

		BasicCredential bc = GetDataByEmailId.getBasicCredentialByUserId(userId);
		BasicProfile pb= GetDataByEmailId.getBasicProfileByEmailId(userId);
		if(pb==null||pb.getUsertype()!=1){
			validation=false;
		}else{
			if((bc == null)||(bc.getDisabledflag()==1))
			{
				validation=false;
			}
			else
			{
				String encriptPass = Encript.doEncript(password);
				if(encriptPass.equals(bc.getPassword()))
				{
					validation=true;
				}
			}
		}
		

	return validation;
}
public boolean getOwnerUser(String userId,String password)
{
	
	boolean validation = false;

		BasicCredential bc = GetDataByEmailId.getBasicCredentialByUserId(userId);
		BasicProfile pb= GetDataByEmailId.getBasicProfileByEmailId(userId);
		if(pb==null||pb.getUsertype()!=3)
		{
			validation=false;
		}else{
			if((bc == null)||(bc.getDisabledflag()==1))
			{
				validation=false;
			}
			else
			{
				String encriptPass = Encript.doEncript(password);
				if(encriptPass.equals(bc.getPassword()))
				{
					validation=true;
				}
			}
		}

	return validation;
}
public boolean getAdminUser(String userId,String password)
{
	
	boolean validation = false;

		BasicCredential bc = GetDataByEmailId.getBasicCredentialByUserId(userId);
		BasicProfile pb= GetDataByEmailId.getBasicProfileByEmailId(userId);
		if(pb==null||pb.getUsertype()!=4){
			validation=false;
		}else{
			if((bc == null)||(bc.getDisabledflag()==1))
			{
				validation=false;
			}
			else
			{
				String encriptPass = Encript.doEncript(password);
				if(encriptPass.equals(bc.getPassword()))
				{
					validation=true;
				}
			}

		}
		
	return validation;
}

public static boolean setPassword(BasicCredential bc) {

    SessionFactory sf = HibernateCoreUtil.getSessionFactory();

  Transaction t = null;

try {

        Session s = sf.openSession();

       t = s.beginTransaction(); // start a new transaction
       	   s.saveOrUpdate(bc);
       t.commit(); // commit transaction

          return true;

} catch (Exception ex) {

System.err.println("Error -->" + ex.getMessage());

if (t != null)

     t.rollback();

    return false;

}

}

public boolean setDisableFlag(BasicCredential bc) {

    SessionFactory sf = HibernateCoreUtil.getSessionFactory();

  Transaction t = null;

try {

	
        Session s = sf.openSession();

       t = s.beginTransaction(); // start a new transaction
       	   s.saveOrUpdate(bc);
       t.commit(); // commit transaction

          return true;

} catch (Exception ex) {

System.err.println("Error -->" + ex.getMessage());

if (t != null)

     t.rollback();

    return false;

}

}

public boolean setCoupenFlag(Coupon coupon) {

    SessionFactory sf = HibernateCoreUtil.getSessionFactory();

  Transaction t = null;

try {	
        Session s = sf.openSession();

       t = s.beginTransaction(); // start a new transaction
       	   s.saveOrUpdate(coupon);
       t.commit(); // commit transaction

          return true;

} catch (Exception ex) {

System.err.println("Error -->" + ex.getMessage());

if (t != null)

     t.rollback();

    return false;

}

}



}